package org.example.project.controller;

import org.example.project.entity.Post;
import org.example.project.service.PostService;
import org.example.project.util.ResponseCode;
import org.example.project.util.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author 岩
 * @Package org.example.project.controller
 * @Project MyApp
 * @Name PostController
 * @Date 2024-04-27 15:02
 * @Filename PostController
 */

@RestController
@RequestMapping("/post")
public class PostController {
    /**
     * 服务对象
     * */
    @Resource
    private PostService postService;

    /**
     * 查询所有文章
     * @return 文章列表
     * */
    @GetMapping
    public ResponseData queryAll() {
        return new ResponseData(ResponseCode.success, this.postService.queryAll());
    }

    /**
     * 根据id查询文章
     * @param id 文章id
     * @return 文章实例
     * */
    @GetMapping("/getPostById")
    public ResponseData getPostById(@RequestParam("id") Integer id){
        return new ResponseData(ResponseCode.success, this.postService.queryById(id));
    }
    /**
     * 根据标题查询文章
     * @param title 文章标题
     * @return 文章列表
     * */
    @GetMapping("/getPostByTitle")
    public ResponseData getPostByTitle(@RequestParam("title") String title){
        return new ResponseData(ResponseCode.success, this.postService.queryByTitle(title));
    }

    /**
     * 新增文章
     * @param post 文章实例
     * @return 新增后的文章实例
     * */
    @PostMapping
    public ResponseData addPost(@RequestBody Post post) {
        return new ResponseData(ResponseCode.success, this.postService.insert(post));
    }

    /**
     * 更新文章
     * @param post 文章实例
     * @return 更新后的文章实例
     * */
    @PutMapping
    public ResponseData updatePost(@RequestBody Post post) {
        return new ResponseData(ResponseCode.success, this.postService.update(post));
    }

    /**
     * 删除文章
     * @param id 文章id
     * @return 删除结果
     * */
    @DeleteMapping
    public ResponseData deletePost(@RequestParam("id") Integer id) {
        return new ResponseData(ResponseCode.success, this.postService.delete(id));
    }

    /**
     * 点赞
     * @param id 文章id
     * @return 点赞结果
     * */
    @PutMapping("/likePost")
    public ResponseData likePost(@RequestParam("id") Integer id) {
        return new ResponseData(ResponseCode.success, this.postService.likePost(id));
    }
}
